<template>
	<view v-if="detail !== null">
		<uni-section title="订单信息" type="line">
			<view class="container margin">
				<view class="text-right margin-right">订单号</view>
				<view>{{ detail.orderNo }}</view>
				<view class="text-right margin-right">总费用</view>
				<view><text class="font-bold font28 uni-warning">{{ detail.totalAmount }}</text>元</view>
				<view class="text-right margin-right">订单状态</view>
				<view><text class="font-bold font28 uni-warning">{{ showOrderStatusDesc(detail.orderStatus) }}</text></view>
			</view>
		</uni-section>
		<uni-section title="航班信息" type="line">
			<view class="container margin" v-for="(item, index) in detail.flights" :key="index">
				<view class="text-right margin-right">出发机场</view>
				<view>{{ item.dportName + item.dterm }}</view>
				<view class="text-right margin-right">到达机场</view>
				<view><text class="font-bold font28 uni-warning">{{ item.aportName + item.aterm }}</text></view>
				<view class="text-right margin-right">出发日期</view>
				<view><text class="font-bold font28 uni-warning">{{ item.ddate+` `+item.dtime+`-`+item.atime }}</text></view>
				<view class="text-right margin-right">航班号</view>
				<view><text class="font-bold font28 uni-warning">{{ item.flightNo }}</text></view>
				<view class="text-right margin-right">舱位</view>
				<view><text class="font-bold font28 uni-warning">{{ item.subclass }}</text></view>
			</view>
		</uni-section>
		<uni-section title="乘机人信息" type="line">
			<view class="container margin" v-for="(item, index) in detail.passengers" :key="index">
				<view class="text-right margin-right">姓名</view>
				<view>{{ item.name }}</view>
				<view class="text-right margin-right">证件号</view>
				<view>{{ item.idNo }}</view>
				<view class="text-right margin-right">手机号</view>
				<view>{{ item.mobile }}</view>
				<view class="text-right margin-right">票号</view>
				<view>{{ item.ticketNo }}</view>
			</view>
		</uni-section>
		<uni-section title="价格信息" type="line">
			<view class="container margin" v-for="(item, index) in detail.prices" :key="index">
				<view class="text-right margin-right">销售价</view>
				<view>{{ item.parValue }}</view>
				<view class="text-right margin-right">税</view>
				<view>{{ item.tax }}</view>
				<view class="text-right margin-right">服务费</view>
				<view>{{ item.serviceCharge }}</view>
				<view class="text-right margin-right">让利</view>
				<view>{{ item.discount }}</view>
			</view>
		</uni-section>
	</view>
</template>

<script>
	import {
		APP_FLIGHT_PATH
	} from "@/common/api/const.js";
	import {
		checkLoginStatus
	} from "@/common/api/user.js"
	import {
		searchFlightOrder,
		searchFlightOrderDetailByOrderNo,
		searchFlightOrderByAuthCode,
	} from "@/common/api/flight.js";
	import {
		payForFlightOrder,
		submitFlightOrder,
		approveFlightOrder,
	} from "@/common/api/flight.js";
	import {
		showFlightOrderStatus,
		showPayType,
		showPsgType,
		showIdTypeDesc,
		showOrderTypeDesc,
		showItineraryType,
		showTicketStatus,
	} from "@/common/api/common.js";
	import {
		createRefundOrderMulti
	} from "@/common/api/flight-refund.js";
	import {
		createChangeOrderMulti
	} from "@/common/api/flight-change.js";
	import {
		saveComment,
		searchComments
	} from "@/common/api/comment.js";

	import PriceInfo from "@/common/api/PriceInfo.js";

	export default {
		data() {
			return {
				detail: null,
				id: 0,
				orderNo: "",
				authCode: null,
				mobile: null,

				adultPrice: new PriceInfo(),
				childPrice: new PriceInfo(),
				infantPrice: new PriceInfo(),

				remark: "",
				commentContent: "",
				comments: [],
				extras: [],
				modalTitle: "",
				modalTitle2: "",
				modalNullable: false,

				btnDisabled: false,
			};
		},
		computed: {
			logined() {
				return this.$store.state.logined;
			},
			isAdmin() {
				return this.$store.getters.isAdmin;
			},
			psgCount: function() {
				if (this.detail !== null) {
					this.priceInfo.ticketCount = this.detail.passengers.length;
					return this.priceInfo.ticketCount;
				} else {
					this.priceInfo.ticketCount = 1;
					return 1;
				}
			},
			isReadOnly: function() {
				return this.authCode !== null;
			},
		},
		onLoad: function(options) {
			this.id = parseInt(options.id);
			this.searchOrderDetail();
		},
		methods: {
			back: function() {
				this.$router.go(-1);
			},
			showErrMsg: function(msg, msgType) {
				this.$store.dispatch("showAlertMsg", {
					errMsg: msg,
					errMsgType: msgType,
				});
			},
			showLoading: function(loadingText) {
				this.$store.commit("showLoading", {
					loading: true,
					loadingText: loadingText,
				});
			},
			hideLoading: function() {
				this.$store.commit("showLoading", {
					loading: false
				});
			},
			showTicketStatus: function(status) {
				return showTicketStatus(status);
			},
			checkLoginStatus: function() {
				checkLoginStatus((v) => {
					console.log(v);
					if (v.status === "OK") {
						const u = {
							username: v.username,
							logined: true,
							fullname: v.fullname,
							gpMode: v.gpMode,
						};
						this.$store.commit("setUsername", u);
					}
				});
			},
			searchOrderDetail: function() {
				console.log("this.authCode: " + this.authCode);
				if (this.authCode !== null) {
					//使用授权码查看订单信息
					const params = {
						authCode: this.authCode,
						mobile: this.mobile,
					};
					searchFlightOrderByAuthCode(this.id, params, (v) => {
						if (v.errcode === 0) {
							this.detail = v;
							this.id = this.detail.id;
							this.setAdultPrice();
						} else {
							this.showErrMsg(v.errmsg, "danger");
						}
					});
					return;
				}
				if (this.id > 0) {
					searchFlightOrder(this.id, (val) => {
						this.detail = val;
						this.setAdultPrice();
						this.searchComments();
					});
				} else {
					searchFlightOrderDetailByOrderNo(this.orderNo, (v) => {
						this.detail = v;
						this.id = this.detail.id;
						this.setAdultPrice();
						this.searchComments();
					});
				}
			},
			showOrderStatusDesc: function(status) {
				return showFlightOrderStatus(status);
			},
			getPayTypeDesc: function(payType) {
				return showPayType(payType);
			},
			showPsgType: function(psgType) {
				return showPsgType(psgType);
			},
			showGender: function(val) {
				switch (val) {
					case 1:
						return "男";
					case 2:
						return "女";
					default:
						return "";
				}
			},
			showIdTypeDesc: function(idType) {
				return showIdTypeDesc(idType);
			},
			showItineraryType: function(itineraryType) {
				return showItineraryType(itineraryType);
			},
			commonShowMessage: function(jsonResult) {
				if (jsonResult.status !== "OK") {
					this.showErrMsg("失败：" + jsonResult.errmsg, "danger");
				} else {
					if (jsonResult.desc !== "") {
						this.showErrMsg(jsonResult.desc);
					} else {
						this.showErrMsg("操作成功");
					}
					this.searchOrderDetail();
				}
			},
			printItinerary: function() {
				const url =
					"/" + APP_FLIGHT_PATH + "/order/flight/" + this.id + "/itinerary";
				window.open(url);
			},
			multiRefundTicket: function(index) {
				// 多人同时退废
				const psgInfo = this.detail.passengers[index];
				const psgType = psgInfo.psgType;

				let psgPrice;
				if (psgType === 0) {
					this.setAdultPrice();
					psgPrice = this.adultPrice;
				} else if (psgType === 1) {
					this.setChildPrice();
					psgPrice = this.childPrice;
				} else if (psgType === 2) {
					this.setInfantPrice();
					psgPrice = this.infantPrice;
				}

				// 找到同类型的乘机人：成人，儿童，婴儿
				const psgList = [];
				for (const info of this.detail.passengers) {
					if (info.psgType === psgType) {
						psgList.push({
							ticketNo: info.ticketNo,
							name: info.name,
							idNo: info.idNo,
							selected: false,
						});
					}
				}

				this.$refs.multiRefundTicketModal
					.modal(
						psgList,
						this.id,
						this.detail.orderNo,
						this.detail.flights,
						this.detail.intlTicket,
						psgPrice.amount,
						psgPrice.cost,
						psgPrice.parvalue,
						psgPrice.tax
					)
					.then((info) => {
						this.doCreateRefundOrderMulti(info);
					})
					.catch((ex) => {});
			},
			doCreateRefundOrderMulti: function(info) {
				createRefundOrderMulti(JSON.stringify(info), (jsonResult) => {
					if (jsonResult.status !== "OK") {
						this.showErrMsg(jsonResult.errmsg, "danger");
					} else {
						this.showErrMsg("操作成功");
						this.$router.push("/flt/refund/order/" + jsonResult.returnCode);
					}
				});
			},
			changeTicket: function(index) {
				// 多人同时签转
				const psgInfo = this.detail.passengers[index];
				const psgType = psgInfo.psgType;

				let psgPrice;
				if (psgType === 0) {
					this.setAdultPrice();
					psgPrice = this.adultPrice;
				} else if (psgType === 1) {
					this.setChildPrice();
					psgPrice = this.childPrice;
				} else if (psgType === 2) {
					this.setInfantPrice();
					psgPrice = this.infantPrice;
				}

				// 找到同类型的乘机人：成人，儿童，婴儿
				const psgList = [];
				let onlyOneTicketAllowed = false;
				for (const info of this.detail.passengers) {
					if (info.psgType === psgType) {
						if (info.ticketNos.length == 0) {
							psgList.push({
								ticketNo: info.ticketNo,
								name: info.name,
								idNo: info.idNo,
								selected: false,
							});
						} else {
							onlyOneTicketAllowed = true;
							for (const ticketNo of info.ticketNos) {
								psgList.push({
									ticketNo: ticketNo,
									name: info.name,
									idNo: info.idNo,
									selected: false,
								});
							}
						}
					}
				}

				const params = {
					orderId: this.id,
					orderNo: this.detail.orderNo,
					intlTicket: this.detail.intlTicket,
					oldPnrNo: this.detail.pnrNo,
					amount: psgPrice.amount,
					cost: psgPrice.cost,
					parvalue: psgPrice.parvalue,
					tax: psgPrice.tax,
					flights: this.detail.flights,
					onlyOneTicketAllowed: onlyOneTicketAllowed,
				};
				this.$refs.changeTicketModal
					.modal(psgList, params)
					.then((v) => this.doCreateChangeOrder(v))
					.catch((ex) => {});
			},
			doCreateChangeOrder: function(info) {
				createChangeOrderMulti(JSON.stringify(info), (jsonResult) => {
					if (jsonResult.status !== "OK") {
						this.showErrMsg(jsonResult.errmsg, "danger");
					} else {
						this.showErrMsg("操作成功");
						this.$router.push("/flt/change/order/" + jsonResult.returnCode);
					}
				});
			},
			setAdultPrice: function() {
				let priceInfo = null;
				// console.log(this.detail.prices);
				for (let i = 0; i < this.detail.prices.length; i++) {
					if (this.detail.prices[i].priceType === 0) {
						priceInfo = this.detail.prices[i];
						break;
					}
				}

				// console.log(priceInfo);

				if (priceInfo === null) {
					return;
				}

				this.adultPrice.price = priceInfo.price;
				this.adultPrice.parValue = priceInfo.parValue;
				this.adultPrice.tax = priceInfo.tax;
				this.adultPrice.insurance = priceInfo.insurance;
				this.adultPrice.serviceCharge = priceInfo.serviceCharge;
				this.adultPrice.commRate = 0;
				this.adultPrice.commission = priceInfo.commission;
				this.adultPrice.discountRate = 0;
				this.adultPrice.discount = priceInfo.discount;
				this.adultPrice.amount = priceInfo.amount;
				this.adultPrice.cost = priceInfo.cost;
				this.adultPrice.ticketCount = this.detail.adultCount;

				// console.log(this.adultPrice);
			},
			setChildPrice: function() {
				this.childPrice.price = this.detail.chdPrice;
				this.childPrice.parValue = this.detail.chdParvalue;
				this.childPrice.tax = this.detail.chdTax;
				this.childPrice.insurance = this.detail.chdInsurance;
				this.childPrice.serviceCharge = this.detail.chdServiceCharge;
				this.childPrice.commRate = 0;
				this.childPrice.commission = this.detail.chdCommission;
				this.childPrice.discountRate = 0;
				this.childPrice.discount = this.detail.chdDiscount;
				this.childPrice.amount = this.detail.chdAmount;
				this.childPrice.cost = this.detail.chdCost;
				this.childPrice.ticketCount = this.detail.childCount;
			},
			setInfantPrice: function() {
				this.infantPrice.price = this.detail.infPrice;
				this.infantPrice.parValue = this.detail.infParvalue;
				this.infantPrice.tax = this.detail.infTax;
				this.infantPrice.insurance = this.detail.infInsurance;
				this.infantPrice.serviceCharge = this.detail.infServiceCharge;
				this.infantPrice.commRate = 0;
				this.infantPrice.commission = this.detail.infCommission;
				this.infantPrice.discountRate = 0;
				this.infantPrice.discount = this.detail.infDiscount;
				this.infantPrice.amount = this.detail.infAmount;
				this.infantPrice.cost = this.detail.infCost;
				this.infantPrice.ticketCount = this.detail.infantCount;
			},
			addExtraService2: function() {
				this.$router.push({
					path: "/vaas/order",
					query: {
						flightOrderId: this.id
					},
				});
			},
			showOrderType: function(orderType) {
				return showOrderTypeDesc(orderType);
			},
			onlinePay: function() {
				payForFlightOrder(this.detail.id, (v) => {
					if (v.status === "OK") {
						this.showErrMsg(
							"如果没有打开在线支付新窗口，请检查您的浏览器是否阻止本网页打开新窗口。",
							"info"
						);
						console.log(v.url);
						$("#paymentForm").empty().html(v.url);
						document.forms["alipaysubmit"].submit();
					} else {
						this.showErrMsg(v.errmsg, "danger");
					}
				});
			},
			submitOrder: function() {
				// this.btnDisabled = true
				this.showLoading("处理中");
				submitFlightOrder(
					this.id,
					(v) => this.commonShowMessage(v),
					() => {
						// this.btnDisabled = false
						this.hideLoading();
					}
				);
			},
			approveOrder: function() {
				this.$refs.confirmModal
					.modal("机票订单审批")
					.then((v) => {
						this.showLoading("处理中");
						const params = {
							denyCode: v.denyCode,
							denyReason: v.denyReason,
						};
						approveFlightOrder(
							this.id,
							params,
							(v) => this.commonShowMessage(v),
							() => this.hideLoading()
						);
					})
					.catch((ex) => {});
			},
			saveComment: function() {
				const params = {
					orderType: 1000,
					orderId: this.id,
					content: this.commentContent,
				};
				saveComment(params, (v) => {
					this.commonShowMessage(v);
					if (v.status === "OK") {
						//刷新评论
						this.searchComments();
					}
				});
			},
			searchComments: function() {
				const params = {
					orderType: 1000,
					orderId: this.id,
				};
				searchComments(params, (v) => {
					this.comments = v.dataList;
				});
			},
		},
	};
	// 874
</script>

<style>
	
</style>
